package com.bosssoft.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bosssoft.baseEnity.SelectItem;
import com.bosssoft.baseParam.BaseParam;
import com.bosssoft.entity.dto.FibSealDTO;
import com.bosssoft.entity.po.FibSealPO;
import com.bosssoft.entity.vo.FibSealVO;
import com.bosssoft.exception.DaoException;
import com.bosssoft.fegin.OssFeginService;
import com.bosssoft.page.PageResult;
import com.bosssoft.response.Result;
import com.bosssoft.service.FibSealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;

/**
 * @author Aria
 */
@Api(tags = "印章管理")
@RestController
@RequestMapping("/seal")
public class FibSealController {

    @Autowired
    FibSealService sealService;

    @Autowired
    OssFeginService ossFeginService;

    @ApiOperation(value = "印章下拉框")
    @PostMapping("/getSealSelectItem")
    public Result getSealSelectItem() {
        try {
            QueryWrapper<FibSealPO> queryWrapper = new QueryWrapper<>();
            queryWrapper.orderByAsc("seal_id");
            List<FibSealPO> fibSealPOS = sealService.getBaseMapper().selectList(queryWrapper);
            SelectItem[] selectItems = new SelectItem[fibSealPOS.size()];
            for (int i = 0; i < fibSealPOS.size(); i++) {
                selectItems[i] = new SelectItem("印章编号: " + fibSealPOS.get(i).getSealId(), fibSealPOS.get(i).getSealId().toString());
            }
            return Result.ok().data("map", selectItems);
        } catch (DaoException e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @ApiOperation(value = "分页查询")
    @PostMapping("/getSealPage")
    public Result getSealPage(@RequestParam(defaultValue = "1") Integer pageNo,
                              @RequestParam(defaultValue = "10") Integer pageSize, FibSealVO fibSealVO) {
        try {
            Page<FibSealPO> page = new Page<>(pageNo, pageSize);
            QueryWrapper<FibSealPO> queryWrapper = new QueryWrapper<>();
            if (ObjectUtil.isNotNull(fibSealVO.getSealId())) {
                queryWrapper.like("seal_id", fibSealVO.getSealId());
            }
            if (ObjectUtil.isNotNull(fibSealVO.getSealName())) {
                queryWrapper.like("seal_name", fibSealVO.getSealName());
            }
            List<FibSealPO> fibSealPOS = sealService.getBaseMapper().selectList(queryWrapper);
            PageResult<FibSealPO> pageResult = new PageResult<>(page, fibSealPOS);
            return Result.ok().data("page", pageResult);
        } catch (DaoException e) {
            e.printStackTrace();
            return Result.error();
        }
    }


    @ApiOperation(value = "新增或更新,不传id是新增,传id是更新")
    @PostMapping("/sealInsertOrUpdate")
    public Result sealInsertOrUpdate(MultipartFile multipartFile, @Validated({BaseParam.add.class}) FibSealVO fibSealVO) {
        FibSealDTO fibSealDTO = new FibSealDTO();
        BeanUtils.copyProperties(fibSealVO, fibSealDTO);
        try {
            Result upload = null;
            if (multipartFile != null) {
                upload = ossFeginService.upload(multipartFile);
            }
            String url = upload.getData().get("url").toString();
            System.out.println(url);
            fibSealDTO.setSealUrl(url);
            sealService.insertOrUpdate(fibSealDTO);
        } catch (
                DaoException e) {
            if (e.getMessage() != null) {
                return Result.error(e.getCode(), e.getMessage());
            }
            return Result.error();
        }
        return Result.ok();
    }

    @ApiOperation(value = "根据id删除")
    @PostMapping("/sealDelete")
    public Result sealDelete(@RequestBody @Validated({BaseParam.delete.class}) FibSealVO fibSealVO) {
        FibSealDTO fibSealDTO = new FibSealDTO();
        BeanUtils.copyProperties(fibSealVO, fibSealDTO);
        try {
            sealService.updateDeleted(fibSealDTO);
        } catch (DaoException e) {
            if (e.getMessage() != null) {
                return Result.error(e.getCode(), e.getMessage());
            }
            return Result.error();
        }
        return Result.ok();
    }

    @ApiOperation(value = "批量删除")
    @PostMapping("/sealDeleteList")
    public Result sealDeleteList(@RequestBody List<FibSealVO> fibSealVOList) {
        FibSealDTO fibSealDTO;
        try {
            for (FibSealVO fibSealVO : fibSealVOList) {
                fibSealDTO = new FibSealDTO();
                BeanUtils.copyProperties(fibSealVO, fibSealDTO);
                sealService.updateDeleted(fibSealDTO);
            }
        } catch (DaoException e) {
            if (e.getMessage() != null) {
                return Result.error(e.getCode(), e.getMessage());
            }
            return Result.error();
        }
        return Result.ok();
    }

    @ApiOperation(value = "查询所有数据")
    @PostMapping("/getSealAll")
    public Result getSealAll() {
        try {
            List<FibSealPO> records = sealService.list();
            return Result.ok().data("list", records);
        } catch (DaoException e) {
            e.printStackTrace();
            return Result.error();
        }
    }


}
